/*
 * 
 * ------------------------
 * 首页结构JavaScript
 * ------------------------
 * 
 */
require(["mmRouter"],function () {
	var model = avalon.define("mean", function(vm) {
		vm.currentTag = 'home'
		vm.currentPage = "home.html";
		vm.loadingVisible = false;//加载提示是否显示
		vm.isLoadPageOver = false;//是否加载完成
		vm.secondMenu = [];//第二头部数据内容
		vm.bodyMarginTop = 50;

		//加載page完成的回調
		vm.loadPageOver = function() {
			vm.$fire("all!init", vm.currentPage);
			scroll(0,0);
			vm.loadingVisible = false;
			vm.isLoadPageOver = true;
		};
		
		//手机上消失menu的方法
		vm.dismissMenu = function(){
			if(window.innerWidth < 640){
				$(".navbar-toggle").click();
			}
		}
		
		//监听当前tag
		vm.$watch('currentTag',function(){
			switch(vm.currentTag){
				case 'my':
					vm.secondMenu = my;
					break;
				case 'manger':
					vm.secondMenu = manger;
					break;
				default:
					vm.secondMenu = [];
					break;
			}
			
			if(vm.secondMenu.length){
				vm.bodyMarginTop = 0;
			}else{
				vm.bodyMarginTop = 50;
			}
		});
	});
	
	
	//监听resize并发布全局广播
	window.onresize = function(){
		model.$fire("all!resize");
	}
	
	
	//我的模块子菜单
	var my = [{title : '我的学生',route:'my/myStudent'},{title : '我的题库',route:'my/myquestions'}];
	//管理模块子菜单
	var manger = [{title : '成员',route:'manger/member'},{title : '班级',route:'manger/class'},{title : '题库设置',route:'manger/question'}];
	//路由的回调方法
	function routeCallBack() {
		var params = this.params;
		var path = this.path;
		
		switch(path){
			case '/':
				path = '/home';
				break;
			case '/my':
				path = '/my/myStudent';
				break;
			case '/manger':
				path = '/manger/member';
				break;
		}
		
		
		var currentPage = path.substr(1) + ".html";
		if(model.currentPage == currentPage)
			return;
			
		model.currentTag = path.split('/')[1];
	    model.currentPage = currentPage;
	    
	    
	    setTimeout(function(){
			model.loadingVisible = !model.isLoadPageOver;
		},1000);
		model.isLoadPageOver = false;
	}

	//五个子界面路由
	avalon.router.get("/", routeCallBack);
	avalon.router.get("/home", routeCallBack);
	avalon.router.get("/information",routeCallBack);
	avalon.router.get("/manger",routeCallBack);
	avalon.router.get("/statistics",routeCallBack);
	
	//我的模块
	avalon.router.get("/my",routeCallBack);
	avalon.router.get("/my/myStudent",routeCallBack);
	avalon.router.get("/my/myquestions",routeCallBack);
	avalon.router.get("/my/video",routeCallBack);
	
	//管理模块
	avalon.router.get("/manger",routeCallBack);
	avalon.router.get("/manger/class",routeCallBack);
	avalon.router.get("/manger/class_detail",routeCallBack);
	avalon.router.get("/manger/member",routeCallBack);
	avalon.router.get("/manger/question",routeCallBack);
	avalon.router.get("/manger/subject",routeCallBack);
	
	//用户设置
	avalon.router.get("/userSetting",routeCallBack);
	
	avalon.history.start({
	    basepath: "/avalon"
	});

	avalon.scan();
	avalon.config({loader: false})
});
